global datadir  "X\codes_AP_aid"

do  "$datadir/codes/macro_var_setup.do"

clear all
*** Input BIS Data (https://www.bis.org/statistics/full_data_sets.htm but retrieved June 2020)
insheet using "$datadir/public_data/bis_june2020.csv", clear names

keep if measure=="Amounts outstanding / Stocks"
keep if balancesheetposition=="Total liabilities"
keep if typeofinstruments=="Loans and deposits"
keep if  currencydenomination=="All currencies"
keep if  currencytypeofreportingcountry=="All currencies (=D+F+U)"

keep if typeofreportinginstitutions=="All reporting banks/institutions (domestic, foreign, consortium and unclassified)"
keep if counterpartysector=="Non-banks, total"
drop if reportingcountry=="All reporting countries"
keep if positiontype=="Cross-border"
drop if counterpartycountry=="All countries"|counterpartycountry=="International organisations"


keep l_rep_cty reportingcountry l_cp_country counterpartycountry t19* t20*
reshape long t, i(l_rep_cty reportingcountry l_cp_country counterpartycountry) j(time) str
replace t="" if t=="NaN"
destring t, replace
replace time="2019q1" if time=="20t19q1"
replace time="2019q2" if time=="20t19q2"
replace time="2019q3" if time=="20t19q3"
replace time="2019q4" if time=="20t19q4"

gen year=substr(time, 1, 4)
gen quarter=substr(time, 6, 1)
destring year, replace
destring quarter, replace
rename reportingcountry target 
rename counterpartycountry source

** to match MONA years ***
keep if year>1983
save "$datadir/constructed/bis.dta", replace


***** FIN DATA (https://www.imf.org/external/np/fin/tad/query.aspx)


insheet using "$datadir/public_data/fin_program_flow_v2.csv", clear names
gen dis_date= date(originaldisbursementdate , "MDY")

format dis_date %td

gen arr_date= date(originalarrangementdate , "MDY")
format arr_date %td

gen quarter=quarter(dis_date)
gen year=year(dis_date)
sort member dis_date

sort member year quarter
egen program_group=group( member originalarrangementdate)
sort program_group dis_date
by program_group: gen n=_n
egen max=max(n), by(program_group)
gen end_datet=dis_date if max==n
egen end_date=max(end_date), by(program_group)
format end_date %td

***** Keep only the disbursement for the review completed
duplicates drop flowtype description member dis_date amount , force
*** collpase to country year quarter and program type
collapse (sum) amount (mean) arr_date end_date program_group, by(year quarter dis_date flowtype description member )
sort member dis_date
drop if amount==0| amount==.
drop if year>2019 
drop if year==2019 & quarter==4
tab year



foreach i in dis arr end{
gen `i'_q=qofd(`i'_date)
format `i'_q %tq
}

** harmonize country names
gen source=proper(member)
replace source="Afghanistan" if source=="Afghanistan, Islamic Republic Of"
replace source="Armenia" if source=="Armenia, Republic Of"
replace source="Cote d'Ivoire" if source=="Cote D'Ivoire"
replace source="Bosnia and Herzegovina" if source=="Bosnia And Herzegovina"
replace source="Congo Democratic Republic" if source=="Congo, Democratic Republic Of"
replace source="Congo" if source=="Congo, Republic Of"
replace source="Gambia" if source=="Gambia, The"
replace source="Tajikistan" if source=="Tajikistan, Republic Of"
replace source="North Macedonia" if source=="North Macedonia, Republic Of"
replace source="Serbia and Montenegro" if source=="Serbia And Montenegro"
replace source="Serbia" if source=="Serbia, Republic Of"
replace source="Yemen" if source=="Yemen, Republic Of"
replace source="Sao Tome and Principe" if source=="Sao Tome & Principe"
replace source="Moldova" if source=="Moldova, Republic Of"
replace source="Belarus" if source=="Belarus, Republic Of"
replace source="Cape Verde" if source=="Cabo Verde"
replace source="Croatia" if source=="Croatia, Republic Of" 
replace source="Estonia" if source=="Estonia, Republic Of"
replace source="Kazakhstan" if source=="Kazakhstan, Republic Of" 
replace source="South Korea" if source=="Korea" 
replace source="Laos" if source=="Lao People'S Democratic Republic" 
replace source="Latvia" if source=="Latvia, Republic Of" 
replace source="Lithuania" if source=="Lithuania, Republic Of" 
replace source="Poland" if source=="Poland, Republic Of"
replace source="Russia" if source=="Russian Federation"
replace source="Slovakia" if source=="Slovak Republic"
replace source="Slovenia" if source=="Slovenia, Republic Of"
replace source="St. Vincent and the Grenadines" if source=="St. Vincent And The Grenadines"
replace source="Trinidad and Tobago" if source=="Trinidad And Tobago"
replace source="Uzbekistan" if source=="Uzbekistan, Republic Of"


*** below for when country has multiple programs at the same time
preserve
keep year amount quarter  source dis_q arr_q end_q description flowtype
duplicates drop year quarter  source dis_q arr_q end_q description, force
encode description, gen(dis_type)
encode flowtype, gen(flow_type)
drop description flowtype 
 label copy dis_type dtype
  label copy flow_type ftype
 bysort source dis_q: gen n=_n
foreach i of numlist 1/3{
gen dis_type`i'=dis_type if n==`i'
gen flow_type`i'=flow_type if n==`i'
}
collapse (max) dis_type1- flow_type3, by( source dis_q)
foreach i of numlist 1/3{
label values dis_type`i' dtype
label values flow_type`i' ftype
}
rename dis_q date
save "$datadir/constructed/fin_type.dta", replace
restore

** a few countries with multiple per quarterly
collapse (sum) amount (mean) arr_q end_q  , by(year quarter  source dis_q)

**** Merge with BIS data
merge 1:m source year quarter using "$datadir/constructed/bis.dta"


*** drop countries not in BIS sample
drop if _m==1
rename amount fin_amount
replace fin_amount=0 if fin_amount==.
drop _m
save "$datadir/constructed/bis.dta", replace


use "$datadir/constructed/bis.dta", clear

**** Haven centers (AJR definition)
gen target_offshore=0
replace target_offshore=1 if target=="Cayman Islands"|target=="Switzerland"|target=="Belgium"|target=="Singapore"|target=="Hong Kong SAR"|target=="Bahamas"|target=="Luxembourg"|target=="Jersey"|target=="Bahrain"|target=="Austria"|target=="Guernsey"|target=="Isle of Man"|target=="Macao SAR"|target=="Panama"|target=="Bermuda"|target=="Netherlands Antilles"

preserve
keep if target_offshore==0
gen nonmissing = !missing(t)
collapse (sum) t nonmissing (mean)  fin_amount arr_q end_q dis_q, by(source year quarter)
replace t=. if nonmissing==0
drop nonmissing
rename t t_no
save "$datadir/constructed/bis_n.dta", replace
restore

keep if target_offshore==1
gen nonmissing = !missing(t)
collapse (sum) t nonmissing (mean)  fin_amount arr_q end_q dis_q, by(source year quarter)
sort source year quarter
replace t=. if nonmissing==0
drop nonmissing
merge 1:1 source year quarter using "$datadir/constructed/bis_n.dta"

** drop yugoslavia
keep if _m==3
drop _m


*** Getting IMF program duration
gen date=yq(year, quarter)
format date %tq
bysort source: carryforward arr_q end_q, replace
gen program=(date>=arr_q & date<=end_q)
save  "$datadir/constructed/bis_n.dta", replace


use "$datadir/constructed/macro", clear
drop if year==2020
*drop if quarter==4 & year==2019
drop if year>=2020
rename country_macro source
merge 1:1 source year quarter using "$datadir/constructed/bis_n.dta"
*** drop countries not in BIS sample
drop if _m==1
drop _m
replace fin_amount=fin_amount/1000000
save  "$datadir/constructed/bis_n.dta", replace


merge 1:1 source date using "$datadir/constructed/fin_type.dta"
*** drop countries not in BIS sample
drop if _m==2
drop _m
save  "$datadir/constructed/bis_n.dta", replace

